/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.gruposp2p.aularest.model.controller;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import org.gruposp2p.aularest.model.Itemcalificable;
import java.util.ArrayList;
import java.util.Collection;
import org.gruposp2p.aularest.model.Itemcalificabletype;
import org.gruposp2p.aularest.model.controller.exceptions.IllegalOrphanException;
import org.gruposp2p.aularest.model.controller.exceptions.NonexistentEntityException;
import org.gruposp2p.aularest.model.controller.exceptions.PreexistingEntityException;

/**
 *
 * @author jj
 */
public class ItemcalificabletypeJpaController {

    public ItemcalificabletypeJpaController() {
        emf = Persistence.createEntityManagerFactory("AulaRest_PU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Itemcalificabletype itemcalificabletype) throws PreexistingEntityException, Exception {
        if (itemcalificabletype.getItemcalificableCollection() == null) {
            itemcalificabletype.setItemcalificableCollection(new ArrayList<Itemcalificable>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Itemcalificable> attachedItemcalificableCollection = new ArrayList<Itemcalificable>();
            for (Itemcalificable itemcalificableCollectionItemcalificableToAttach : itemcalificabletype.getItemcalificableCollection()) {
                itemcalificableCollectionItemcalificableToAttach = em.getReference(itemcalificableCollectionItemcalificableToAttach.getClass(), itemcalificableCollectionItemcalificableToAttach.getId());
                attachedItemcalificableCollection.add(itemcalificableCollectionItemcalificableToAttach);
            }
            itemcalificabletype.setItemcalificableCollection(attachedItemcalificableCollection);
            em.persist(itemcalificabletype);
            for (Itemcalificable itemcalificableCollectionItemcalificable : itemcalificabletype.getItemcalificableCollection()) {
                Itemcalificabletype oldItemcalificabletypeIdOfItemcalificableCollectionItemcalificable = itemcalificableCollectionItemcalificable.getItemcalificabletypeId();
                itemcalificableCollectionItemcalificable.setItemcalificabletypeId(itemcalificabletype);
                itemcalificableCollectionItemcalificable = em.merge(itemcalificableCollectionItemcalificable);
                if (oldItemcalificabletypeIdOfItemcalificableCollectionItemcalificable != null) {
                    oldItemcalificabletypeIdOfItemcalificableCollectionItemcalificable.getItemcalificableCollection().remove(itemcalificableCollectionItemcalificable);
                    oldItemcalificabletypeIdOfItemcalificableCollectionItemcalificable = em.merge(oldItemcalificabletypeIdOfItemcalificableCollectionItemcalificable);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findItemcalificabletype(itemcalificabletype.getId()) != null) {
                throw new PreexistingEntityException("Itemcalificabletype " + itemcalificabletype + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Itemcalificabletype itemcalificabletype) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Itemcalificabletype persistentItemcalificabletype = em.find(Itemcalificabletype.class, itemcalificabletype.getId());
            Collection<Itemcalificable> itemcalificableCollectionOld = persistentItemcalificabletype.getItemcalificableCollection();
            Collection<Itemcalificable> itemcalificableCollectionNew = itemcalificabletype.getItemcalificableCollection();
            List<String> illegalOrphanMessages = null;
            for (Itemcalificable itemcalificableCollectionOldItemcalificable : itemcalificableCollectionOld) {
                if (!itemcalificableCollectionNew.contains(itemcalificableCollectionOldItemcalificable)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Itemcalificable " + itemcalificableCollectionOldItemcalificable + " since its itemcalificabletypeId field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Itemcalificable> attachedItemcalificableCollectionNew = new ArrayList<Itemcalificable>();
            for (Itemcalificable itemcalificableCollectionNewItemcalificableToAttach : itemcalificableCollectionNew) {
                itemcalificableCollectionNewItemcalificableToAttach = em.getReference(itemcalificableCollectionNewItemcalificableToAttach.getClass(), itemcalificableCollectionNewItemcalificableToAttach.getId());
                attachedItemcalificableCollectionNew.add(itemcalificableCollectionNewItemcalificableToAttach);
            }
            itemcalificableCollectionNew = attachedItemcalificableCollectionNew;
            itemcalificabletype.setItemcalificableCollection(itemcalificableCollectionNew);
            itemcalificabletype = em.merge(itemcalificabletype);
            for (Itemcalificable itemcalificableCollectionNewItemcalificable : itemcalificableCollectionNew) {
                if (!itemcalificableCollectionOld.contains(itemcalificableCollectionNewItemcalificable)) {
                    Itemcalificabletype oldItemcalificabletypeIdOfItemcalificableCollectionNewItemcalificable = itemcalificableCollectionNewItemcalificable.getItemcalificabletypeId();
                    itemcalificableCollectionNewItemcalificable.setItemcalificabletypeId(itemcalificabletype);
                    itemcalificableCollectionNewItemcalificable = em.merge(itemcalificableCollectionNewItemcalificable);
                    if (oldItemcalificabletypeIdOfItemcalificableCollectionNewItemcalificable != null && !oldItemcalificabletypeIdOfItemcalificableCollectionNewItemcalificable.equals(itemcalificabletype)) {
                        oldItemcalificabletypeIdOfItemcalificableCollectionNewItemcalificable.getItemcalificableCollection().remove(itemcalificableCollectionNewItemcalificable);
                        oldItemcalificabletypeIdOfItemcalificableCollectionNewItemcalificable = em.merge(oldItemcalificabletypeIdOfItemcalificableCollectionNewItemcalificable);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = itemcalificabletype.getId();
                if (findItemcalificabletype(id) == null) {
                    throw new NonexistentEntityException("The itemcalificabletype with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Itemcalificabletype itemcalificabletype;
            try {
                itemcalificabletype = em.getReference(Itemcalificabletype.class, id);
                itemcalificabletype.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The itemcalificabletype with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Itemcalificable> itemcalificableCollectionOrphanCheck = itemcalificabletype.getItemcalificableCollection();
            for (Itemcalificable itemcalificableCollectionOrphanCheckItemcalificable : itemcalificableCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Itemcalificabletype (" + itemcalificabletype + ") cannot be destroyed since the Itemcalificable " + itemcalificableCollectionOrphanCheckItemcalificable + " in its itemcalificableCollection field has a non-nullable itemcalificabletypeId field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(itemcalificabletype);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Itemcalificabletype> findItemcalificabletypeEntities() {
        return findItemcalificabletypeEntities(true, -1, -1);
    }

    public List<Itemcalificabletype> findItemcalificabletypeEntities(int maxResults, int firstResult) {
        return findItemcalificabletypeEntities(false, maxResults, firstResult);
    }

    private List<Itemcalificabletype> findItemcalificabletypeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Itemcalificabletype as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Itemcalificabletype findItemcalificabletype(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Itemcalificabletype.class, id);
        } finally {
            em.close();
        }
    }

    public int getItemcalificabletypeCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from Itemcalificabletype as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
